//
//  QTawardVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/9/5.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QTawardVC.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"

@interface QTawardVC (){
    
    NSDictionary *_dic;
    
    
}

@property (weak, nonatomic) IBOutlet UIImageView *scanCodeImageView;

@property (weak, nonatomic) IBOutlet UILabel *activeLabel;


@end

@implementation QTawardVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.textString isEqualToString:@"1"]) {
        self.title = @"推荐有奖";
        [self requestData];
    }
    if ([self.textString isEqualToString:@"2"]) {
        self.title = @"邀请好友";
        [self testRequestData];
    }
    
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(bac)];
    
}

- (void)bac{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)wechatAction:(id)sender {
    CLog(@"微信");
    if ([self.textString isEqualToString:@"2"]) {
        
        [UMSocialData defaultData].extConfig.wechatSessionData.title = _dic[@"title"];
        [UMSocialData defaultData].extConfig.wechatSessionData.url = _dic[@"htmlUrl"];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:_dic[@"activityContent"] image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
    if ([self.textString isEqualToString:@"1"]) {
        [UMSocialData defaultData].extConfig.wechatSessionData.title = _dic[@"title"];
        [UMSocialData defaultData].extConfig.wechatSessionData.url = _dic[@"qrCode"];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:_dic[@"data"] image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
    
}

- (IBAction)wechatFridenAction:(id)sender {
    CLog(@"朋友圈");
    if ([self.textString isEqualToString:@"2"]) {
        [UMSocialData defaultData].extConfig.wechatTimelineData.title = _dic[@"title"];
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = _dic[@"htmlUrl"];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:_dic[@"activityContent"] image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
    if ([self.textString isEqualToString:@"1"]) {
        [UMSocialData defaultData].extConfig.wechatTimelineData.title = _dic[@"title"];
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = _dic[@"qrCode"];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:_dic[@"data"] image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
}

- (IBAction)qqActoin:(id)sender {
    CLog(@"QQ");
    if ([self.textString isEqualToString:@"2"]) {
        [UMSocialData defaultData].extConfig.qqData.title = _dic[@"title"];
        [UMSocialData defaultData].extConfig.qqData.url = _dic[@"htmlUrl"];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:_dic[@"activityContent"] image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
    if ([self.textString isEqualToString:@"1"]) {
        [UMSocialData defaultData].extConfig.qqData.title = _dic[@"title"];
        [UMSocialData defaultData].extConfig.qqData.url = _dic[@"qrCode"];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:_dic[@"data"] image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
}

- (IBAction)messageAction:(id)sender {
    CLog(@"短信");
    if ([self.textString isEqualToString:@"2"]) {
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToSms] content:[NSString stringWithFormat:@"%@%@",_dic[@"title"],_dic[@"htmlUrl"]] image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
    if ([self.textString isEqualToString:@"1"]) {
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToSms] content:[NSString stringWithFormat:@"%@%@",_dic[@"title"],_dic[@"qrCode"]] image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                CLog(@"分享成功！");
            }
        }];
    }
}

- (void)testRequestData{
    
    NSString *url = [QParamService urlWithAction:@"getInviterActivityInfo"];
    NSDictionary *param = [QParamService getInviterActivityInfo];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return;
        }
        NSLog(@"dic:%@",dict);
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            _dic = basModel.items;
            
            if ([self.textString isEqualToString:@"1"]) {
                _activeLabel.text = _dic[@"data"];
            }
            else if ([self.textString isEqualToString:@"2"]) {
                _activeLabel.text = _dic[@"showContent"];
                //                _activeLabel.text = @"邀请好友注册油到了即刻奖励150元奖励券";
            }
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errMsg isEqualToString:@"请重新登录"]){
                [QDataUtility deleteLoginModel];
            }
        }
    }];
}

- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"getQrCode"];
    NSDictionary *param = [QParamService awardWithType:@"1"];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            _dic = basModel.items;
            if ([self.textString isEqualToString:@"1"]) {
                _activeLabel.text = _dic[@"data"];
            }
            else if ([self.textString isEqualToString:@"2"]) {
                _activeLabel.text = _dic[@"showContent"];
                
            }
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errMsg isEqualToString:@"请重新登录"]){
                [QDataUtility deleteLoginModel];
            }
        }
    }];
}

@end
